<?php
/**
 * Index控制器
 *
 * @author		黑冰 <954650386@qq.com>
 */

use \waf\Waf;
use \waf\log\WLog;
use \waf\controller\WController;
use \waf\client\pools\WMysqlPools;
use \waf\client\pools\WRedisPools;
use \waf\client\monitor\WMonitorClient;
use \waf\context\WContext;

class IndexController extends WController
{
    public function indexAction() 
    {
            echo "index";
            $arrGet     = $this->getRequest()->getParam('get',  array());
            $arrPost    = $this->getRequest()->getParam('post', array());
            $arrParams  = $this->getRequest()->getParams();
            print_r($arrGet);
            print_r($arrPost);
            print_r($arrParams);
    }

    public function testMysqlAction()
    {
        echo "login";
        $ret = MUser::doLogin('root', 'root');
        print_r($ret);
        echo 'ok';
    }

    public function testRedisAction()
    {
        echo "redis";
        $ret = MUser::getConfig("foo");
        echo $ret;
    }

    public function testTcpAction()
    {
        $client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
        if (!$client->connect('127.0.0.1', 29090, 0.5))
        {
            exit("connect failed. Error: {$client->errCode}\n");
        }
        $client->send('{"url":"", "seq":"1", "param1":"p1", "param2":"p2"}'."\r\n");
        echo $client->recv();
        $client->close();
    }

    public function testUdpAction()
    {
        $client = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);
        if (!$client->connect('127.0.0.1', 19090, 0.5))
        {
            exit("connect failed. Error: {$client->errCode}\n");
        }
        $client->send('{"url":"", "seq":"1", "param1":"p1", "param2":"p2"}');
        echo $client->recv();
        $client->close();
    }

    public function testMysql2Action()
    {
        $conf = Waf::getConfig('db', 'mysql');
        $objWMysqlClient = WMysqlPools::pop($conf);

        //=========== 纯sql用法
        // insert
        /*
        if($objWMysqlClient->execute("insert into mytest2 values ('', 'name4', ?);", [20]))
        {
            echo $objWMysqlClient->getLastInsertId();
        }else{
            echo "fail";
        }*/

        // delete
        /*
        if($objWMysqlClient->execute("delete from mytest2 where id = ?", [3]))
        {
            echo 'ok';
        }else{
            echo "fail";
        }*/

        // update
        /*
        if($objWMysqlClient->execute("update mytest2 set pass = 'passaa' where id = ? and `name` = ?", [6, 'name4']))
        {
            echo 'ok';
        }else{
            echo "fail";
        }*/

        // find
        /*
        print_r($objWMysqlClient->findAll("select * from mytest2"));
        print_r($objWMysqlClient->find("select * from mytest2"));
        print_r($objWMysqlClient->findAll("select * from mytest2 where id = ?", [6]));
        */

        //============= 便捷用法
        // insert
        /*
        $data   = array(
            'name'  => time(),
            'f1'    => 1,
        );

        if($objWMysqlClient->insert('mytest2', $data)->execute())
        {
            echo $objWMysqlClient->getLastInsertId();
        }else{
            echo 'fail';
        }*/

        // update
        /*
        $arrUpdateData  = array(
            'f1'    => 100,
            'f2'    => 200,
        );
        $where          = "where id = 9";
        $arrWhereParams = array();
        // 查看底层拼接的sql和参数
        print_r($objWMysqlClient->update('mytest2', $arrUpdateData, $where, $arrWhereParams)->getSqlAndParams());
        if($objWMysqlClient->update('mytest2', $arrUpdateData, $where, $arrWhereParams)->execute())
        {
            echo 'ok';
        }else{
            echo 'fail';
        }*/

        // delete
        /*
        if($objWMysqlClient->delete('mytest2', "where id = ?", [7])->execute())
        {
            echo 'ok';
        }else{
            echo 'fail';
        }*/

        // select
        //print_r($objWMysqlClient->select('mytest2', '*', "where id = ?", [8])->findAll());
    }

    // 测试事务
    public function testMysql3Action()
    {
        $conf = Waf::getConfig('db', 'mysql');
        $objWMysqlClient = WMysqlPools::pop($conf);

        $objWMysqlClient->begin();
        try{
            $data   = array(
                'name'  => time(),
                'f1'    => 1,
            );

            if($objWMysqlClient->insert('mytest2', $data)->execute())
            {
                echo $objWMysqlClient->getLastInsertId();
            }else{
                echo 'fail';
            }

            $objWMysqlClient->commit();
        }catch (Exception $e)
        {
            $objWMysqlClient->rollback();
        }
    }
    public function testRedis2Action()
    {
        $conf = Waf::getConfig('db', 'redis');
        $objWRedisClient = WRedisPools::pop($conf);
        $objWRedisClient->set('key', time());
        print_r($objWRedisClient->get('key'));
    }

    public function testCallReportAction()
    {

    }

    public function testCountReportAction()
    {
        WMonitorClient::coutReport(__METHOD__, 'testCount');
    }

    public function testLogAction()
    {
        WLog::logInfo("in.", __FILE__, __LINE__, __FUNCTION__);
        $cid        = WContext::getCid();
        $traceId    = WContext::getTraceId();
        echo "testLost:{$cid}|{$traceId}";
    }
}